import sys
import threading
import time

from ys_python.common.base.yprint import print_delimiter
from ys_python.common.mq.mq_model import MQBaseModule, defult_MQModuleCfg


def test_topic_operate():
    mq = __get_mq()
    print(sys.getrefcount(mq))
    threading.Thread(target=mq.run).start()
    time.sleep(2)
    print_delimiter("增加话题")
    time.sleep(1)
    mq.add_topic("test")
    time.sleep(2)
    print_delimiter("重复增加")
    time.sleep(1)
    mq.add_topic("test")
    time.sleep(2)
    print_delimiter("删除话题")
    time.sleep(1)
    mq.del_topic("test")
    time.sleep(2)
    print_delimiter("重复删除")
    time.sleep(1)
    mq.del_topic("test")
    time.sleep(2)
    mq.stop()


def test_key_interrupt():
    mq = __get_mq()
    try:
        print_delimiter("请使用 Ctrl + C 退出")
        mq.run()
    except KeyboardInterrupt:
        mq.stop()


def __get_mq():
    cfg = defult_MQModuleCfg()
    cfg.name = "test"
    return MQBaseModule(cfg)


if __name__ == '__main__':
    test_topic_operate()
    test_key_interrupt()
